<?php

namespace app\api\controller\canvas;

use app\admin\model\Wish;
use app\api\controller\Base;
use app\admin\model\Egao;
use think\Request;

/**
 * 随你搞接口
 */
class Egaos extends Base
{

    public $control_version = '0.0.0';
    public $version;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->version = $request->param('version');
    }

    // 获取banner
    public function banner()
    {
        if ($this->version == $this->control_version) {
            $data = [];
        } else {
            $data = array(
                array(
                    'url' => '/pages/egao/egao?id=31&title=春晚入场券',
                    'target' => 'self',
                    'appid' => '',
                    'path' => '',
                    'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/item/chunwan.jpg',
                ),
                array(
                    'url' => '',
                    'name' => '一起飞飞飞',
                    'target' => 'miniProgram',
                    'appid' => 'wxe42ac5dd0f116b26',
                    'path' => '',
                    'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/banner/fly.jpg'
                ),
                array(
                    'url' => '',
                    'name' => '阅享每日一文',
                    'target' => 'miniProgram',
                    'appid' => 'wx9a21a5ca27f8cbc5',
                    'path' => '',
                    'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/banner/mryw.jpg'
                ),
                array(
                    'url' => '',
                    'name' => '拯救查尔斯',
                    'target' => 'miniProgram',
                    'appid' => 'wxc76853fdcf188c3e',
                    'path' => '',
                    'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/banner/charles.jpg'
                ),
            );
        }
        $this->result('banner', $data);
    }

    // 获取分类
    public function category()
    {
        $data = array(
            array(
                'index' => 0,
                'id' => 'zb',
                'title' => '装逼',
            ),
            array(
                'index' => 1,
                'id' => 'rm',
                'title' => '热门',
            ),
            array(
                'index' => 2,
                'id' => 'tj',
                'title' => '推荐',
            ),
        );
        $this->result('category', $data);
    }

    // 根据分类获取数据
    public function get_list()
    {
        $type = $this->request->param('type') ?: 'zb';
        if ($this->version == $this->control_version) {
            $data['zhufu'] = false;
            $data['list'] = Egao::where('type', $type)->where('control', 0)->order('id', 'desc')->where('delstatus', 0)->select();
        } else {
            $version_str = intval(str_replace('.', '', $this->version));
            $data['zhufu'] = $type == 'rm' ? true : false;
            $data['list'] = Egao::where(['type' => $type, 'delstatus' => 0])->where('version', '<=', $version_str)->order('id', 'desc')->select();
        }
        $this->result('list', $data);
    }

    // 通过id获取恶搞类型
    public function get($id)
    {
        $egao = Egao::get($id);
        if ($egao) {
            $egao->config = json_decode($egao->config, true);
            $egao->qrcode_config = json_decode($egao->qrcode_config, true);
            $this->result('egao', $egao);
        }
    }

    // 获取config
    public function config()
    {
        $zhufuimg = 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/banner/zhufu.jpg';
        $shareflag = true;
        $hua = '图片已经保存到手机相册，将图片分享朋友圈恶搞一下~';
        $btn_str = '邀请好友一起搞';

        if ($this->version == $this->control_version) {
            $shareflag = false;
            $hua = '已保存';
            $btn_str = '炫耀一下';
        }

        $flags = array(
            'shareflag' => $shareflag,
            'zhufuimg' => $zhufuimg,
            'hua' => $hua,
            'btn_str' => $btn_str,
        );

        $this->result('ok', $flags);
    }

    // 购车画图接口
    public function buycar()
    {
        $type = $this->request->param('type');
        $name = $this->request->param('name');

        $root_path = ROOT_PATH . "public/uploads/cdn/images/suinigao/buycar/"; // 购买豪车根路径

        $date = date('Ymd') . '/'; // 当天标识

        if ($this->version == $this->control_version) {
            $source = $root_path . "noqrcode/" . $type . '.jpg';
        } else {
            $source = $root_path . "qrcode/" . $type . '.jpg';
        }

        $img_path = $date . md5($source . $name) . '.jpg'; // 生成图片路径

        if (!file_exists($root_path . $img_path)) {

            $main = imagecreatefromjpeg($source);
            $width = imagesx($main);
            $height = imagesy($main);

            $target = imagecreatetruecolor($width, $height);

            $white = imagecolorallocate($target, 255, 255, 255);
            imagefill($target, 0, 0, $white);
            // 重新采样拷贝部分图像并调整大小
            imagecopyresampled($target, $main, 0, 0, 0, 0, $width, $height, $width, $height);

            // 字的RGB颜色
            $fontColor = imagecolorallocate($target, 54, 54, 54);

            // 画名称
            $fontSize = 18;
            $font = ROOT_PATH . 'public/assets/fonts/aatianzhen.ttf'; // 字体
            $fontBox = imagettfbbox($fontSize, 0, $font, $name); // 文字水平居中实质
            imagettftext($target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2 + 75), 1310, $fontColor, $font, $name); // 画字
            imagettftext($target, $fontSize, -4, 325, 740, $fontColor, $font, $name); // 画字
            // 画签订日期
            $fontSize = 13;
            $text = date('Y    m    d');
            $font = ROOT_PATH . 'public/assets/fonts/stsong.ttf';
            $fontColor = imagecolorallocate($target, 43, 43, 43);
            $fontBox = imagettfbbox($fontSize, 0, $font, $text);
            imagettftext($target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2 + 18), 1347, $fontColor, $font, $text);
            imagettftext($target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2 - 246), 1350, $fontColor, $font, $text);
            // 画交车日期
            $fontSize = 11;
            $text = date('Y     m     d', time() + 20 * 86400);
            $fontBox = imagettfbbox($fontSize, 0, $font, $text);
            imagettftext($target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2 - 31), 1053, $fontColor, $font, $text);
            imagettftext($target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2 - 160), 1143, $fontColor, $font, $text);

            imageantialias($target, true); //抗锯齿，有些PHP版本有问题，谨慎使用

            if (!file_exists($root_path . $date)) {
                @mkdir($root_path . $date);
            }
            imagejpeg($target, $root_path . $img_path);
            imagedestroy($main);
            imagedestroy($target);

        }

        $this->result($name, 'https://xcx.nanxuncn.cn/uploads/cdn/images/suinigao/buycar/' . $img_path);
    }

    // 祝福语相关
    public function get_wish_info()
    {
        $item_list = array(
            array(
                'id' => 1,
                'name' => '长辈',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/zhangbei.png',
            ),
            array(
                'id' => 2,
                'name' => '师长',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/shizhang.png',
            ),
            array(
                'id' => 3,
                'name' => '领导',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/lingdao.png',
            ),
            array(
                'id' => 4,
                'name' => '父母',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/tongshi.png',
            ),
            array(
                'id' => 5,
                'name' => '朋友',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/pengyou.png',
            ),
            array(
                'id' => 6,
                'name' => '恋人',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/lianren.png',
            ),
            array(
                'id' => 7,
                'name' => '晚辈',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/wanbei.png',
            ),
            array(
                'id' => 8,
                'name' => '同事',
                'img' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/suinigao/jierizhufu/header/qianren.png',
            ),
        );
        $festival_list = array(
            array(
                'id' => 1,
                'name' => '普通祝福',
            ),
            array(
                'id' => 2,
                'name' => '中秋节',
            ),
            array(
                'id' => 3,
                'name' => '国庆节',
            ),
            array(
                'id' => 4,
                'name' => '春节',
            ),
        );
        $list = array(
            'item_list' => $item_list,
            'festival_list' => $festival_list,
        );
        $this->result('list', $list);
    }

    // 随机一个祝福
    public function get_random_wish($r, $f)
    {
        $wishs1 = Wish::where(['type_r' => $r, 'type_f' => $f])->select();
        $wishs2 = Wish::where(['type_r' => '0', 'type_f' => $f])->select();
        $wishs = array_merge($wishs1, $wishs2);
        $wish = $wishs[array_rand($wishs)];
        $this->result('random', $wish->content);
    }

    // 获取全部祝福
    public function get_all_wish($r, $f)
    {
        $wishs1 = Wish::where(['type_r' => $r, 'type_f' => $f])->select();
        $wishs2 = Wish::where(['type_r' => '0', 'type_f' => $f])->select();
        $wishs = array_merge($wishs1, $wishs2);
        $this->result('all', $wishs);
    }

}
